package com.zzxx.photograph.dao.impl;

import com.zzxx.photograph.dao.ImgDao;
import com.zzxx.photograph.domain.Img;
import com.zzxx.photograph.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ImgDaoImpl implements ImgDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findCountByIname(String itag) {
        StringBuilder _sql = new StringBuilder("select count(*) from img where 1=1");
        List<Object> params = new ArrayList<>();
        _sql.append(" and itag like ? ");
        params.add("%" + itag + "%");

        return jdbcTemplate.queryForObject(_sql.toString(), params.toArray(), Integer.class);
    }

    @Override
    public List<Img> findByIname(String itag, int start, int pageSize) {
        StringBuilder _sql = new StringBuilder("select * from img where 1=1");
        List<Object> params = new ArrayList<>();
        _sql.append(" and itag like ? ");
        params.add("%" + itag + "%");
        _sql.append("limit ?,?");
        params.add(start);
        params.add(pageSize);
        return jdbcTemplate.query(_sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(Img.class));
    }

    @Override
    public int findCount() {
        String sql = "select count(*) from img ";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Img> findAll(int start, int pageSize) {
        String sql = "select * from img order by idate desc limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Img.class), start, pageSize);
    }

    @Override
    public Img findImgByRid(int iid) {
        // 在img中通过iid查询一个img对象存到list中
        String sql = "select * from img where iid = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Img.class), iid).get(0);
    }

    @Override
    //根据Iid查询对应图片的信息
    public Img findImgByIid(int iid) {
        String sql = "select * from img where iid=?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Img.class), iid).get(0);
    }

    @Override
    public void addClick(int iid) {
        String sql = "update img set iclick=iclick+1 where iid=?";
        jdbcTemplate.update(sql,iid);
    }

    @Override
    public String findSrcByIid(int iid) {
        String sql = "select isrc from img where iid = ?";
        return jdbcTemplate.queryForObject(sql,String.class,iid);
    }

    @Override
    public int findCountsByIname(Map<String, String[]> condition) {
        StringBuilder _sql = new StringBuilder("select count(*) from img where 1=1");
        List<Object> params = new ArrayList<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {
            if (key.equals("iname")) {
                _sql.append(" and itag like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            }
        }

        return jdbcTemplate.queryForObject(_sql.toString(), params.toArray(), Integer.class);

    }

    @Override
    public List<Img> findByInames(Map<String, String[]> condition, int start, int pageSize) {
        StringBuilder _sql = new StringBuilder("select * from img where 1=1");
        List<Object> params = new ArrayList<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {
            if (key.equals("iname")) {
                _sql.append(" and itag like ? ");
                params.add("%" + condition.get(key)[0] + "%");
            }
        }
        _sql.append("limit ?,?");
        params.add(start);
        params.add(pageSize);
        return jdbcTemplate.query(_sql.toString(), params.toArray(), new BeanPropertyRowMapper<>(Img.class));

    }

    @Override
    public void addImg(Img img) {
        String sql="insert into img values(?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,img.getIid(),img.getIsrc(),img.getIname(),img.getItag()
        ,img.getIclick(),img.getCid(),img.getAid(),img.getIdate(),img.getIsize());
    }

    @Override
    public int findCountsByAid(int aid) {
        String sql="select count(*) from img where aid=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,aid);
    }

    @Override
    public List<Img> findImgByAid(int aid, int start, int pageSize) {
        String sql="select * from img  where aid=? order by  idate  desc limit ?,?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Img.class),aid,start,pageSize);
    }

    @Override
    public Integer findMaxId() {
        String sql="select max(iid) from img ";
        return jdbcTemplate.queryForObject(sql,Integer.class);
    }

    @Override
    public void updateImg(Img img) {
        String sql ="update img set iname=?,itag=?,cid=?,idate=? where iid=?";
        jdbcTemplate.update(sql,img.getIname(),img.getItag(),img.getCid(),img.getIdate(),img.getIid());
    }

    @Override
    public void deleteImg(Integer iid) {
        String sql="delete from img where iid=?";
        jdbcTemplate.update(sql,iid);
    }
}
